package com.yisoo.ubag.iot.manager;


import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.PubRequest;
import com.aliyuncs.iot.model.v20180120.PubResponse;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("sendDataManager")
public class SendDataManager {
    @Autowired
    IAcsClient client;
    //User 自定义topic
    public boolean sendByUser(String productKey,String deviceName,String subTopic,String content){
        PubRequest request = new PubRequest();
        request.setQos(0);
        //设置发布消息的topic
        request.setTopicFullName("/" + productKey + "/" + deviceName + "/user/"+subTopic);
        request.setProductKey(productKey);
        //设置消息的内容，一定要用base64编码，否则乱码
        request.setMessageContent(Base64.encodeBase64String(content.getBytes()));

        try {
            PubResponse response = client.getAcsResponse(request);
            return response.getSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
